std::is_array
From Cppreference
| C++ Standard Library | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Utilities library | ||||||||||||||||||||||||||||
| Type support | ||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||
| Defined in header <type_traits>
|
||
| template< class T >
struct is_array; |
(C++11 feature) | |
If T is an array type, provides the member constant value equal true. For any other type, value is false.
Contents |
Inherited from std::true_type or std::false_type
Member objects
| value | true if T is an array type, false otherwise (public member object) |
[edit] Equivalent definition
template<class T> struct is_array : std::false_type {}; template<class T> struct is_array<T[]> : std::true_type {}; template<class T, std::size_t N> struct is_array<T[N]> : std::true_type {}; |
[edit] Example
the following example uses is_array to differentiate between an array and a pointer
#include <iostream> #include <type_traits> template<typename A> void describe(const A&, typename std::enable_if<std::is_array<A>::value>::type* = 0) { std::cout << "A " << std::rank<A>::value << "D array\n"; } template<typename A> void describe(const A&, typename std::enable_if<std::is_pointer<A>::value>::type* = 0) { std::cout << "A pointer\n"; } int main() { char s1[] = "test1"; describe(s1); const char* s2 = "test2"; describe(s2); // describe(s1[0]); // compile-time error }
Output:
A 1D array A pointer
[edit] See also
|
obtains the number of dimensions of an array type (class template) |
||
|
obtains the size of an array type along a specified dimension (class template) |
||
|
removes one extent from the given array type (class template) |
||
|
removes all extents from the given array type (class template) |
||